package com.biz.service.weixin;

import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;


import com.biz.utils.ConfigUtil;
import org.springframework.stereotype.Service;

import com.biz.model.Pmodel.weixin.TextMessage;
import com.framework.dao.mdao.DaoSupport;
import com.framework.util.Const;
import com.framework.util.NewLogger;
import com.framework.util.weixin.AesException;
import com.framework.util.weixin.MessageUtil;
import com.framework.util.weixin.SHA1;

/**
 * 处理微信发来的信息
 * 
 * @author GengLong
 *
 */
@Service("wxService")
public class WXService
{
	@Resource(name = "daoSupport")
	private DaoSupport dao;


	
	
	/**
	 * 验证URL
	 * 
	 * @param msgSignature
	 *            签名串，对应URL参数的msg_signature
	 * @param timeStamp
	 *            时间戳，对应URL参数的timestamp
	 * @param nonce
	 *            随机串，对应URL参数的nonce
	 * @param echoStr
	 *            随机串，对应URL参数的echostr
	 * 
	 * @return 解密之后的echostr
	 * @throws AesException
	 *             执行失败，请查看该异常的错误码和具体的错误信息
	 */
	public String verifyUrl(String msgSignature, String token,
			String timeStamp, String nonce, String echoStr) throws AesException
	{
		String signature = SHA1.getSHA1(token, timeStamp, nonce);
		if (!signature.equals(msgSignature))
		{
			throw new AesException(AesException.ValidateSignatureError);
		}
		String result = echoStr;
		return result;
	}

	public String processRequest(String msg)
	{
		String respMessage = null;
		try
		{
			// 默认返回的文本消息内容
			String respContent = "";

			// xml请求解析
			Map<String, String> requestMap = MessageUtil.parseXml(msg);

			// 发送方帐号（open_id）
			String open_id = requestMap.get("FromUserName");
			// 公众帐号
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");

			// 回复文本消息
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(open_id);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setFuncFlag(0);

			// 文本消息
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT))
			{
				respContent = "您好,请问有什么可以帮助您?";
				textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_KF);
			}
			// 事件推送
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT))
			{
				// 事件类型
				String eventType = requestMap.get("Event");
				// 自定义菜单点击事件
				if (eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_CLICK))
				{
					// 事件KEY值，与创建自定义菜单时指定的KEY值对应
					String eventKey = requestMap.get("EventKey");
					if ("kf".equals(eventKey))
					{
						respContent = "客服电话: 4009600651";
					}
				} else if (eventType
						.equalsIgnoreCase(MessageUtil.EVENT_TYPE_UNSUBSCRIBE))
				{
					
					System.out.println("取消关注:");
				} else if (eventType
						.equalsIgnoreCase(MessageUtil.EVENT_TYPE_SUBSCRIBE))// 关注事件				
				{
					//if (isExistOpenId(open_id))
					//{
					//	respContent = "众花感谢您的再次关注!您之前的账号信息我们已为您保留,众花将竭诚为您服务!";
					//} else{
						//String eventKeytest = requestMap.get("EventKey");//带参的参数
						
						respContent = "众花感谢您的关注!众花将竭诚为您服务!点此立即去注册\n"
							 +
							 "<a href=\""+ ConfigUtil.get("SYS_URL")+"/client_toMyInfo.do\">点此免费注册</a>";
					//}
				}
				else if(eventType
						.equalsIgnoreCase("user_get_card")){ //卡券领取事件推送
					
				}
				else if(eventType
						.equalsIgnoreCase("card_pass_check") ){ //卡券审核事件通过
					
				}
				else if(eventType
						.equalsIgnoreCase("card_not_pass_check")){ //卡券审核事件不通过					
					
				}
				else if(eventType
						.equalsIgnoreCase("user_del_card")){ //删除卡券
					
				}
				else if (eventType
						.equalsIgnoreCase("card_merchant_check_result"))
				{ // 子商户审核事件					
					
				} else if (eventType.equalsIgnoreCase("poi_check_notify"))
				{ // 门店新增审核事件					
					
				}
			} else
			{
				respContent = "您好,请问有什么可以帮助您?";
				textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_KF);
			}

			textMessage.setContent(respContent);
			respMessage = MessageUtil.textMessageToXml(textMessage);
		} catch (Exception e)
		{
			//NewLogger.appendLog(msg,Const.WXTSLog_File,null);
			
			e.printStackTrace();
			//System.out.println(e);
			respMessage = "有异常了。。。";
		}
		return respMessage;
	}

	

	
	
	
	
	
	
}
